/*
 * ViewFrame.java
 *
 * Created on 21 novembre 2008, 18:56
 */

package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import model.*;
import util.SpaceUtilities;

/**
 *
 * @author  DolWolf
 */
public class ViewFrame extends javax.swing.JFrame {

    /** Creates new form ViewFrame */
    public ViewFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel10 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jPanel14 = new javax.swing.JPanel();
        jPanel15 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jPanel7 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        jPanel11 = new javax.swing.JPanel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jPanel12 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton8 = new javax.swing.JButton();
        jPanel16 = new javax.swing.JPanel();
        jTextField8 = new javax.swing.JTextField();
        jButton9 = new javax.swing.JButton();
        jTextField9 = new javax.swing.JTextField();
        jButton10 = new javax.swing.JButton();
        jTextField10 = new javax.swing.JTextField();
        jButton11 = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        jSlider1 = new javax.swing.JSlider();
        jLabel6 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel13 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.BorderLayout(5, 5));

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Size of the grid"));

        jLabel3.setText("X :");
        jPanel5.add(jLabel3);

        jTextField2.setText("50");
        jTextField2.setMinimumSize(new java.awt.Dimension(20, 20));
        jTextField2.setPreferredSize(new java.awt.Dimension(40, 20));
        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });
        jPanel5.add(jTextField2);

        jLabel4.setText("Y :");
        jPanel5.add(jLabel4);

        jTextField3.setText("50");
        jTextField3.setPreferredSize(new java.awt.Dimension(40, 20));
        jPanel5.add(jTextField3);

        jPanel3.add(jPanel5);

        jButton3.setText("Create grid");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel6.add(jButton3);

        jButton4.setText("Clear");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel6.add(jButton4);

        jPanel3.add(jPanel6);

        jPanel1.add(jPanel3);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Resources"));

        jLabel1.setText("Resource1 :");
        jPanel4.add(jLabel1);

        jTextField1.setText("200");
        jTextField1.setPreferredSize(new java.awt.Dimension(40, 20));
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jPanel4.add(jTextField1);

        jLabel2.setText("Resource2 :");
        jPanel4.add(jLabel2);

        jTextField4.setText("200");
        jTextField4.setPreferredSize(new java.awt.Dimension(40, 20));
        jPanel4.add(jTextField4);

        jLabel5.setText("Resource3 :");
        jPanel4.add(jLabel5);

        jTextField5.setText("200");
        jTextField5.setPreferredSize(new java.awt.Dimension(40, 20));
        jTextField5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField5ActionPerformed(evt);
            }
        });
        jPanel4.add(jTextField5);

        jButton1.setText("Initialize resources");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel4.add(jButton1);

        jPanel1.add(jPanel4);

        getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Ants"));
        jPanel2.setPreferredSize(new java.awt.Dimension(180, 112));

        jPanel10.setLayout(new java.awt.GridBagLayout());

        jLabel7.setText("X :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        jPanel10.add(jLabel7, gridBagConstraints);

        jTextField6.setText("0");
        jTextField6.setPreferredSize(new java.awt.Dimension(40, 20));
        jPanel10.add(jTextField6, new java.awt.GridBagConstraints());

        jLabel8.setText("Y :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        jPanel10.add(jLabel8, gridBagConstraints);

        jTextField7.setText("0");
        jTextField7.setPreferredSize(new java.awt.Dimension(40, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        jPanel10.add(jTextField7, gridBagConstraints);

        jPanel2.add(jPanel10);

        jButton6.setText("Add this ant");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton6);

        jButton7.setText("Add random ant");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton7);

        jPanel14.setBorder(javax.swing.BorderFactory.createTitledBorder("Histograms"));
        jPanel14.setLayout(new javax.swing.BoxLayout(jPanel14, javax.swing.BoxLayout.Y_AXIS));

        jPanel15.setLayout(new java.awt.GridLayout(3, 2));

        jLabel10.setText("Resource r1 :");
        jPanel15.add(jLabel10);
        jPanel15.add(jLabel11);

        jLabel12.setText("Resource r2 :");
        jPanel15.add(jLabel12);
        jPanel15.add(jLabel13);

        jLabel14.setText("Resource r3 :");
        jPanel15.add(jLabel14);
        jPanel15.add(jLabel15);

        jPanel14.add(jPanel15);

        jPanel2.add(jPanel14);

        getContentPane().add(jPanel2, java.awt.BorderLayout.WEST);

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder("Actions"));
        jPanel7.setPreferredSize(new java.awt.Dimension(200, 300));

        jPanel9.setLayout(new java.awt.GridBagLayout());

        jPanel11.setBorder(javax.swing.BorderFactory.createTitledBorder("Grid Options"));
        jPanel11.setLayout(new javax.swing.BoxLayout(jPanel11, javax.swing.BoxLayout.Y_AXIS));

        jRadioButton1.setText("Activate Pheromones");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });
        jPanel11.add(jRadioButton1);

        jRadioButton2.setText("Vanishable Pheromones");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });
        jPanel11.add(jRadioButton2);

        jRadioButton3.setText("Activate Traces");
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });
        jPanel11.add(jRadioButton3);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        jPanel9.add(jPanel11, gridBagConstraints);

        jPanel7.add(jPanel9);

        jPanel12.setBorder(javax.swing.BorderFactory.createTitledBorder("Orientation\n"));
        jPanel12.setPreferredSize(new java.awt.Dimension(180, 150));
        jPanel12.setLayout(new javax.swing.BoxLayout(jPanel12, javax.swing.BoxLayout.Y_AXIS));

        jScrollPane1.setPreferredSize(new java.awt.Dimension(40, 50));

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "None", "North", "South", "West", "East" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.setMaximumSize(new java.awt.Dimension(100, 100));
        jList1.setMinimumSize(new java.awt.Dimension(20, 30));
        jList1.setPreferredSize(new java.awt.Dimension(40, 40));
        jScrollPane1.setViewportView(jList1);

        jPanel12.add(jScrollPane1);

        jButton8.setText("Set Orientation Field");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });
        jPanel12.add(jButton8);

        jPanel7.add(jPanel12);

        jPanel16.setBorder(javax.swing.BorderFactory.createTitledBorder("Coefficients Option"));
        jPanel16.setLayout(new java.awt.GridBagLayout());

        jTextField8.setText("0.05");
        jTextField8.setPreferredSize(new java.awt.Dimension(40, 20));
        jPanel16.add(jTextField8, new java.awt.GridBagConstraints());

        jButton9.setText("Set Orientation Coefficient");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        jPanel16.add(jButton9, gridBagConstraints);

        jTextField9.setText("0.01");
        jTextField9.setPreferredSize(new java.awt.Dimension(40, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        jPanel16.add(jTextField9, gridBagConstraints);

        jButton10.setText("Set Pheromone Coefficient");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        jPanel16.add(jButton10, gridBagConstraints);

        jTextField10.setText("2500");
        jTextField10.setPreferredSize(new java.awt.Dimension(40, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        jPanel16.add(jTextField10, gridBagConstraints);

        jButton11.setText("Set Vanish Time");
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        jPanel16.add(jButton11, gridBagConstraints);

        jPanel7.add(jPanel16);

        getContentPane().add(jPanel7, java.awt.BorderLayout.EAST);

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder("Commands"));

        jSlider1.setMaximum(1000);
        jSlider1.setMinimum(-1000);
        jSlider1.setValue(100);
        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider1StateChanged(evt);
            }
        });
        jPanel8.add(jSlider1);

        jLabel6.setText("Number of steps : 0");
        jPanel8.add(jLabel6);

        jButton2.setText("Run");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel8.add(jButton2);

        jButton5.setText("Stop");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jPanel8.add(jButton5);

        getContentPane().add(jPanel8, java.awt.BorderLayout.SOUTH);

        jScrollPane2.setViewportView(jPanel13);

        getContentPane().add(jScrollPane2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField2ActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
	if (this.isRunning){
		runner.kill();
	}
	this.jPanel13.removeAll();
	this.jPanel13.updateUI();
	this.jPanel13.revalidate();
	this.jPanel13.repaint();
	this.grid = null;
	this.jRadioButton1.setSelected(false);
	this.jRadioButton2.setSelected(false);
	this.jRadioButton3.setSelected(false);
	this.isPheromoneActivated = false;
	this.isPheromoneVanishable = false;
	this.isTracesActivated = false;
	this.jLabel11.setText(""+0);
	this.jLabel13.setText(""+0);
	this.jLabel15.setText(""+0);
	this.nbStep = 0;
	this.jLabel6.setText("Number of steps : 0");
}//GEN-LAST:event_jButton4ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
	if (this.isRunning){
		runner.kill();
		isRunning = false;
	}
	String xString = this.jTextField2.getText().replaceAll(" ", "");
	String yString = this.jTextField3.getText().replaceAll(" ", "");
		
	if (!this.isInteger(xString)){
		JOptionPane.showMessageDialog(null, "X doit etre un entier");
	} else if (!this.isInteger(yString)){
		JOptionPane.showMessageDialog(null, "Y doit etre un entier");
	} else {
		this.jRadioButton1.setSelected(false);
		this.jRadioButton2.setSelected(false);
		this.jRadioButton3.setSelected(false);
		this.isPheromoneActivated = false;
		this.isPheromoneVanishable = false;
		this.isTracesActivated = false;
		Integer x = Integer.parseInt(xString);
		Integer y = Integer.parseInt(yString);
		universe = new Space2D(x, y);
		this.grid = new Grid(this.universe, this.isPheromoneActivated);
		if (this.isTracesActivated){
			this.grid.setTracesActivated(true);
		}
		//this.gridv2 = new Gridv2(universe);
		System.out.print("Grid cree de taille : "+x+":"+y);
		this.jPanel13.removeAll();
		this.jPanel13.add(grid);
		grid.setPreferredSize(new Dimension(1+17*grid.nbColumns, 1+17*grid.nbRows));
		this.jPanel13.updateUI();
		this.jPanel13.revalidate();
		this.jPanel13.repaint();
		this.jLabel11.setText(""+0);
		this.jLabel13.setText(""+0);
		this.jLabel15.setText(""+0);
		this.nbStep = 0;
		this.jLabel6.setText("Number of steps : 0");
		
	}
}//GEN-LAST:event_jButton3ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
	if ((this.grid!=null)&&(!isRunning)){
		runner = new RunThread(this.grid, this.universe, this.jSlider1.getValue(), this.nbStep, this.jLabel6, this.jLabel11, this.jLabel13, this.jLabel15,this.vanishTime);
		runner.start();
		isRunning = true;
	}
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
	if (isRunning){
		this.nbStep = runner.kill();
		isRunning = false;
	}
}//GEN-LAST:event_jButton5ActionPerformed

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
// TODO add your handling code here:
	if (this.grid == null){
			JOptionPane.showMessageDialog(null, "Vous n'avez pas encore cree de grille");
			return;
		}
		
		boolean reRun = false;
		if (this.isRunning){
			this.nbStep = runner.kill();
			isRunning = false;
			reRun = true;
		}
		
		SpaceUtilities.initRandomAnt(this.universe, this.isPheromoneActivated, this.orientationCoef, this.pheromoneCoef, this.attrationField);
		this.jPanel13.updateUI();
		this.jPanel13.revalidate();
		this.jPanel13.repaint();
		
		if (reRun){
			runner = new RunThread(this.grid, this.universe, this.jSlider1.getValue(), this.nbStep, this.jLabel6, this.jLabel11, this.jLabel13, this.jLabel15,this.vanishTime);
			runner.start();
			isRunning = true;
		}
}//GEN-LAST:event_jButton7ActionPerformed

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
	
	if (this.grid == null){
			JOptionPane.showMessageDialog(null, "Vous n'avez pas encore cree de grille");
			return;
		}
		
		boolean reRun = false;
		if (this.isRunning){
			this.nbStep = runner.kill();
			isRunning = false;
			reRun = true;
		}
		
		String xString = this.jTextField6.getText().replaceAll(" ", "");
		String yString = this.jTextField7.getText().replaceAll(" ", "");
		
		if (!this.isInteger(xString)){
			JOptionPane.showMessageDialog(null, "X doit etre un entier");
		} else if (!this.isInteger(yString)){
			JOptionPane.showMessageDialog(null, "Y doit etre un entier");
		} else {
			Integer x = Integer.parseInt(xString);
			if (x >= this.universe.getDimX()){
				JOptionPane.showMessageDialog(null, "X est superieur a la taille de la grille");
				return;
			}
			Integer y = Integer.parseInt(yString);
			if (y >= this.universe.getDimY()){
				JOptionPane.showMessageDialog(null, "Y est superieur a la taille de la grille");
				return;
			}
			Point loc = new Point(x,y);
			Cell cell = this.universe.getCell(loc);
			if (cell!=null){
				if (!cell.containsAnt()){
					Ant ant;
					try {
						ant = new Ant(this.universe, loc, this.orientationCoef, this.pheromoneCoef, this.attrationField);
						if (this.isPheromoneActivated){
							ant.setPheromoneActivated(true);
						}
						this.universe.addAnt(ant);
						this.jPanel13.updateUI();
						this.jPanel13.revalidate();
						this.jPanel13.repaint();
						System.out.print("Ant ajoute a : "+x+"-"+y);
					} catch (Exception ex) {
						Logger.getLogger(ViewFrame.class.getName()).log(Level.SEVERE, null, ex);
					}			
				} else {
					JOptionPane.showMessageDialog(null, "Une fourmi se trouve deja ici");
					return;
				}
			} else {
				Ant ant;
				try {
					ant = new Ant(this.universe, loc, this.orientationCoef, this.pheromoneCoef, this.attrationField);
					if (this.isPheromoneActivated){
						ant.setPheromoneActivated(true);
					}
					this.universe.addAnt(ant);
					this.jPanel13.updateUI();
					this.jPanel13.revalidate();
					this.jPanel13.repaint();
					System.out.print("Ant ajoute a : "+x+"-"+y);
				} catch (Exception ex) {
					Logger.getLogger(ViewFrame.class.getName()).log(Level.SEVERE, null, ex);
				}			
			}	
		}
		
		if (reRun){
			runner = new RunThread(this.grid, this.universe, this.jSlider1.getValue(), this.nbStep, this.jLabel6, this.jLabel11, this.jLabel13, this.jLabel15,this.vanishTime);
			runner.start();
			isRunning = true;
		}
}//GEN-LAST:event_jButton6ActionPerformed

private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider1StateChanged
	int tsleep = this.jSlider1.getValue();
	if (this.isRunning){
		runner.setTSleep(tsleep);
	}
}//GEN-LAST:event_jSlider1StateChanged

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
	
	if (this.grid == null){
			JOptionPane.showMessageDialog(null, "Vous n'avez pas encore cree de grille");
			return;
		}
		
		boolean reRun = false;
		if (this.isRunning){
			this.nbStep = runner.kill();
			isRunning = false;
			reRun = true;
		}
		
		String r1String = this.jTextField1.getText().replaceAll(" ", "");
		String r2String = this.jTextField4.getText().replaceAll(" ", "");
		String r3String = this.jTextField5.getText().replaceAll(" ", "");
	
	if (!this.isInteger(r1String)){
		JOptionPane.showMessageDialog(null, "Le nombre de resource de type 1 doit etre un entier");
	} else if (!this.isInteger(r2String)){
		JOptionPane.showMessageDialog(null, "Le nombre de resource de type 2 doit etre un entier");
	} else if (!this.isInteger(r3String)){
		JOptionPane.showMessageDialog(null, "Le nombre de resource de type 3 doit etre un entier");
	} else {
		Integer r1 = Integer.parseInt(r1String);
		Integer r2 = Integer.parseInt(r2String);
		Integer r3 = Integer.parseInt(r3String);
		int tot = r1+r2+r3;
		int test = 0;
		for (int i=0; i<this.universe.getDimX();i++){
			for (int j=0; j<this.universe.getDimY();j++){
				Point location = new Point(i,j);
				Cell cell = this.universe.getCell(location);
				if (cell == null){
					test++;
				} else {
					if (!cell.containsResource()){
						test++;
					}
				}
			}
		}
		if (tot>test){
			JOptionPane.showMessageDialog(null, "Il n'y a pas assez de places libres pour disposer toutes ces ressources");
		} else {
			SpaceUtilities.initResourcesAtRandom(r1, r2, r3, this.universe);
			this.jPanel13.updateUI();
			this.jPanel13.revalidate();
			this.jPanel13.repaint();
		}
	}
		
		
		if (reRun){
			runner = new RunThread(this.grid, this.universe, this.jSlider1.getValue(), this.nbStep, this.jLabel6, this.jLabel11, this.jLabel13, this.jLabel15,this.vanishTime);
			runner.start();
			isRunning = true;
		}
	
	
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed

	if (this.grid == null){
			JOptionPane.showMessageDialog(null, "Vous n'avez pas encore cree de grille");
			return;
		}
		
		boolean reRun = false;
		if (this.isRunning){
			this.nbStep = runner.kill();
			isRunning = false;
			reRun = true;
		}
		
		String orientation = (String)this.jList1.getSelectedValue();
		
		this.attrationField = orientation;
		ArrayList<Ant> ants = this.universe.getListAnts();
		for(Ant ant : ants){
			ant.setAttractionField(this.attrationField);
		}
		
		if (reRun){
			runner = new RunThread(this.grid, this.universe, this.jSlider1.getValue(), this.nbStep, this.jLabel6, this.jLabel11, this.jLabel13, this.jLabel15,this.vanishTime);
			runner.start();
			isRunning = true;
		}
}//GEN-LAST:event_jButton8ActionPerformed

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField1ActionPerformed

private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField5ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField5ActionPerformed

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
// TODO add your handling code here:
	if (this.grid == null){
			JOptionPane.showMessageDialog(null, "Vous n'avez pas encore cree de grille");
			return;
		}
		
		boolean reRun = false;
		if (this.isRunning){
			this.nbStep = runner.kill();
			isRunning = false;
			reRun = true;
		}
		
		this.isPheromoneActivated = this.jRadioButton1.isSelected();
		this.grid.setPheromoneActivated(this.isPheromoneActivated);
		this.universe.setPheromoneActivated(this.isPheromoneActivated);
		
		if (reRun){
			runner = new RunThread(this.grid, this.universe, this.jSlider1.getValue(), this.nbStep, this.jLabel6, this.jLabel11, this.jLabel13, this.jLabel15,this.vanishTime);
			runner.start();
			isRunning = true;
		}
}//GEN-LAST:event_jRadioButton1ActionPerformed

private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
// TODO add your handling code here:
	if (this.grid == null){
			JOptionPane.showMessageDialog(null, "Vous n'avez pas encore cree de grille");
			return;
		}
		
		boolean reRun = false;
		if (this.isRunning){
			this.nbStep = runner.kill();
			isRunning = false;
			reRun = true;
		}
		
		this.isPheromoneVanishable = this.jRadioButton2.isSelected();
		this.universe.setPheromoneVanishable(this.isPheromoneVanishable);
		
		if (reRun){
			runner = new RunThread(this.grid, this.universe, this.jSlider1.getValue(), this.nbStep, this.jLabel6, this.jLabel11, this.jLabel13, this.jLabel15,this.vanishTime);
			runner.start();
			isRunning = true;
		}
}//GEN-LAST:event_jRadioButton2ActionPerformed

private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
// TODO add your handling code here:
	if (this.grid == null){
			JOptionPane.showMessageDialog(null, "Vous n'avez pas encore cree de grille");
			return;
		}
		
		boolean reRun = false;
		if (this.isRunning){
			this.nbStep = runner.kill();
			isRunning = false;
			reRun = true;
		}
		
		this.isTracesActivated = this.jRadioButton3.isSelected();
		this.grid.setTracesActivated(this.isTracesActivated);
		
		if (reRun){
			runner = new RunThread(this.grid, this.universe, this.jSlider1.getValue(), this.nbStep, this.jLabel6, this.jLabel11, this.jLabel13, this.jLabel15,this.vanishTime);
			runner.start();
			isRunning = true;
		}
}//GEN-LAST:event_jRadioButton3ActionPerformed

private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
// TODO add your handling code here:
	if (this.grid == null){
			JOptionPane.showMessageDialog(null, "Vous n'avez pas encore cree de grille");
			return;
		}
		
		boolean reRun = false;
		if (this.isRunning){
			this.nbStep = runner.kill();
			isRunning = false;
			reRun = true;
		}
		
		String coeff = this.jTextField8.getText().replaceAll(" ", "");
		
		if (!this.isDouble(coeff)){
			JOptionPane.showMessageDialog(null, "Le coefficient doit etre un reel positif");
		} else {
			double coef = Double.parseDouble(coeff);
			if (coef<0){
				JOptionPane.showMessageDialog(null, "Le coefficient doit etre un reel positif");
			} else {
				this.orientationCoef = coef;
				ArrayList<Ant> ants = this.universe.getListAnts();
				for(Ant ant : ants){
					ant.setOrientationCoef(this.orientationCoef);
				}
			}
		}
		
		if (reRun){
			runner = new RunThread(this.grid, this.universe, this.jSlider1.getValue(), this.nbStep, this.jLabel6, this.jLabel11, this.jLabel13, this.jLabel15,this.vanishTime);
			runner.start();
			isRunning = true;
		}
}//GEN-LAST:event_jButton9ActionPerformed

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
	if (this.grid == null){
			JOptionPane.showMessageDialog(null, "Vous n'avez pas encore cree de grille");
			return;
		}
		
		boolean reRun = false;
		if (this.isRunning){
			this.nbStep = runner.kill();
			isRunning = false;
			reRun = true;
		}
		
		String coeff = this.jTextField9.getText().replaceAll(" ", "");
		
		if (!this.isDouble(coeff)){
			JOptionPane.showMessageDialog(null, "Le coefficient doit etre un reel positif");
		} else {
			double coef = Double.parseDouble(coeff);
			if (coef<0){
				JOptionPane.showMessageDialog(null, "Le coefficient doit etre un reel positif");
			} else {
				ArrayList<Ant> ants = this.universe.getListAnts();
				for(Ant ant : ants){
					ant.setPheromoneCoef(coef);
				}
			}
		}
		
		if (reRun){
			runner = new RunThread(this.grid, this.universe, this.jSlider1.getValue(), this.nbStep, this.jLabel6, this.jLabel11, this.jLabel13, this.jLabel15,this.vanishTime);
			runner.setVanishTime(this.vanishTime);
			runner.start();
			isRunning = true;
		}
}//GEN-LAST:event_jButton10ActionPerformed

private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed
	
	if (this.grid == null){
			JOptionPane.showMessageDialog(null, "Vous n'avez pas encore cree de grille");
			return;
		}
		
		boolean reRun = false;
		if (this.isRunning){
			this.nbStep = runner.kill();
			isRunning = false;
			reRun = true;
		}
		
		String coeff = this.jTextField10.getText().replaceAll(" ", "");
		
		if (!this.isDouble(coeff)){
			JOptionPane.showMessageDialog(null, "Le coefficient doit etre un entier positif");
		} else {
			int coef = Integer.parseInt(coeff);
			if (coef < 0) {
				JOptionPane.showMessageDialog(null, "Le coefficient doit etre un entier positif");
			} else {
				this.vanishTime = coef;
			}
		}
		
		if (reRun){
			runner = new RunThread(this.grid, this.universe, this.jSlider1.getValue(), this.nbStep, this.jLabel6, this.jLabel11, this.jLabel13, this.jLabel15,this.vanishTime);
			runner.start();
			isRunning = true;
		}
	
}//GEN-LAST:event_jButton11ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ViewFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel15;
    private javax.swing.JPanel jPanel16;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jTextField9;
    // End of variables declaration//GEN-END:variables
	Space2D universe;
	Grid grid;
	boolean isRunning = false;
	RunThread runner;
	int nbStep;
	int vanishTime = 2500;
	double orientationCoef = 0.05;
	double pheromoneCoef = 0.01;
	String attrationField = "None";
	boolean isPheromoneActivated = false;
	boolean isPheromoneVanishable = false;
	boolean isTracesActivated = false;
	
	// Test si le string represente un nombre entier
    public boolean isInteger(String chaine) {
		try {
			Integer.parseInt(chaine);
		} catch (NumberFormatException e){
			return false;
		}
		return true;
	}
	
	// Test si le string represente un nombre reel
    public boolean isDouble(String chaine) {
		try {
			Double.parseDouble(chaine);
		} catch (NumberFormatException e){
			return false;
		}
		return true;
	}
}
